﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PuroGame.Business;
using Core.DatabaseAccess;

namespace PuroGame.Repositories.Repository
{
    public class MercadaoRepository : Interfaces.IMercadaoRepository
    {
        public void Criar(mercadao _mercadao)
        {
            string query = @"INSERT INTO mercadao (usuario_id, texto, preco, titulo) 
            VALUES (@usuario_id, @texto, @preco, @titulo)";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("usuario_id", _mercadao.usuario_id);
            param.Add("texto", _mercadao.texto);
            param.Add("preco", _mercadao.preco);
            param.Add("titulo", _mercadao.titulo);

            Base.Save(query, param);
        }

        public List<Business.mercadao> ListarPor(int top, string order, string by)
        {
            string query = @"SELECT TOP(@top) * FROM mercadao ORDER BY @order @by";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("top", top);
            param.Add("order", order);
            param.Add("by", by);

            return Base.Query<mercadao>(query, param).ToList();
        }

        public List<Business.mercadao> ListarPorUsuario(int usuario_id)
        {
            string query = @"SELECT * FROM mercadao WHERE usuario_id = @usuario_id ORDER BY id DESC";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("usuario_id", usuario_id);

            return Base.Query<mercadao>(query, param).ToList();
        }

        public List<mercadao> Paginar(int top, int page)
        {
            string query = @"SELECT TOP(@top) * FROM mercadao ORDER BY id DESC";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("top", top);
            param.Add("page", page);

            return Base.Query<mercadao>(query, param).ToList();
        }


        public void Atualizar(mercadao _mercadao)
        {
            throw new NotImplementedException();
        }

        public mercadao Carregar(int id)
        {
            string query = @"SELECT * FROM mercadao WHERE id = @id";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("id", id);

            return Base.Query<mercadao>(query, param).FirstOrDefault();
        }
    }
}
